package top.callname.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

import top.callname.bean.ConstanstBean;
import top.callname.bean.NoticeBean;
import top.callname.bean.NoticePageList;

/**
 * 公告
 * @author chenwk
 *
 */
public class NoticeDao extends BaseDao{

	
	Logger logger = Logger.getLogger(NoticeDao.class);
	
	
	public boolean deleteById(Integer id){
		String sql = "delete from t_notice where id="+id;
		logger.debug("删除公告:"+sql);
		return adu(sql);
	}
	
	/**
	 * 新增公告记录
	 * @param noticeBean
	 * @return boolean
	 */
	public boolean insertNotice(NoticeBean noticeBean){
		logger.debug(noticeBean.toString());
		String sql = "insert into t_notice (content,createtime,cids,pid,send_time,notice_status) "
				+ "values ('"+noticeBean.getContent()+"',"
						+ "NOW(),"
						+ "'"+noticeBean.getCids()+"',"
						+ ""+noticeBean.getPid()+","
						+ "'"+noticeBean.getSendTime()+"' , " 
						+ noticeBean.getStatus()+ " )";
		logger.debug("新增公告记录:"+sql);
		return adu(sql);
	}
	
	/**
	 * 查询最新的一条
	 * @param cid
	 * @return
	 */
	public NoticeBean selectNewNoticeByCid(Integer cid){
		String sql =  "select  id,content,createtime,cids,pid,send_time,notice_status "
				+ "  from t_notice where notice_status=0 and cids like '%"+cid+"%' order by id desc limit 0,1";
		res = query(sql);
		NoticeBean bean = new NoticeBean();
		try {
			if(res.next()){
				bean.setId(res.getInt("id"));
				bean.setContent(res.getString("content"));
				bean.setPid(res.getInt("pid"));
				bean.setCids(res.getString("cids"));
				bean.setStatus(res.getInt("notice_status"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				String sendTime = res.getString("send_time");
				bean.setSendTime(sendTime.substring(0,sendTime.length()-2));
				bean.setStatusName(ConstanstBean.NOTICE_STATUS_MAP.get(bean.getStatus()));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_notice 出错："+e);
		}
		return bean;
	}
	
	public NoticeBean selectByPrimaryKey(Integer id){
		String sql =  "select  id,content,createtime,cids,pid,send_time,notice_status "
				+ "  from t_notice where id="+id;
		res = query(StringEscapeUtils.escapeSql(sql));
		NoticeBean bean = new NoticeBean();
		try {
			if(res.next()){
				bean.setId(res.getInt("id"));
				bean.setContent(res.getString("content"));
				bean.setPid(res.getInt("pid"));
				bean.setCids(res.getString("cids"));
				bean.setStatus(res.getInt("notice_status"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				String sendTime = res.getString("send_time");
				bean.setSendTime(sendTime.substring(0,sendTime.length()-2));
				bean.setStatusName(ConstanstBean.NOTICE_STATUS_MAP.get(bean.getStatus()));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_notice 出错："+e);
		}
		return bean;
	}
	
	
	/**
	 * 查询所有未发送的任务
	 * @param id
	 * @return NoticeBean
	 */
	public List<NoticeBean> selectListByStatus(Integer status){
		String sql =  "select  id,content,createtime,cids,pid,send_time,notice_status "
				+ "  from t_notice where notice_status="+status;
		res = query(StringEscapeUtils.escapeSql(sql));
		List<NoticeBean> list = new ArrayList<NoticeBean>();
		logger.info("查询待发送或已发送的公告："+sql);
		try {
			while(res.next()){
				NoticeBean bean = new NoticeBean();
				bean.setId(res.getInt("id"));
				bean.setContent(res.getString("content"));
				bean.setPid(res.getInt("pid"));
				bean.setCids(res.getString("cids"));
				bean.setStatus(res.getInt("notice_status"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				String sendTime = res.getString("send_time");
				bean.setSendTime(sendTime.substring(0,sendTime.length()-2));
				bean.setStatusName(ConstanstBean.NOTICE_STATUS_MAP.get(bean.getStatus()));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_notice 出错："+e);
		}
		return list;
	}
	
	
	/**
	 * 更新状态为已发送
	 * @param id
	 * @return boolean
	 */
	public boolean updateNoticeStatus(Integer id){
		String sql =  "update t_notice set  send_time=NOW() , notice_status=1  where id="+id;
		return adu(sql);
	}
	
	
	/**
	 * 
	 * Description:分页查询全部
	 * @param pid
	 * @return List<ClassBean>
	 */
	public NoticePageList selectPageByPid(int pageNow , Integer pid){
		NoticePageList pageList = new NoticePageList();
		pageList.setPageNow(pageNow);
		//查询总记录
		int totalSize = 0 ;
		totalSize = getCount("t_notice"," where pid="+pid);
		pageList.setTotalSize(totalSize);
		ArrayList<NoticeBean> list = new ArrayList<NoticeBean>();
		String limit = " order by id desc limit "+pageList.getStart()+","+pageList.getOffset();
		String sql =   "select  id,content,createtime,cids,pid,send_time,notice_status "
				+ "  from t_notice where pid="+pid+limit;
		logger.info("分页查询公告记录信息："+sql);
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				NoticeBean bean = new NoticeBean();
				bean.setId(res.getInt("id"));
				bean.setContent(res.getString("content"));
				bean.setPid(res.getInt("pid"));
				bean.setCids(res.getString("cids"));
				bean.setStatus(res.getInt("notice_status"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				String sendTime = res.getString("send_time");
				bean.setSendTime(sendTime.substring(0,sendTime.length()-2));
				bean.setStatusName(ConstanstBean.NOTICE_STATUS_MAP.get(bean.getStatus()));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_notice 出错："+e);
		}
		pageList.setList(list);
		return pageList;
	}
	
	
}
